/**
 * 表单校验规则创建规范
 * @author xiufu.wang
 */
import resolveDyncDate, { moment } from './resolve-dyncdate'
import { objectProperty } from './util'
import Schema from 'mars-pro-async-validator';
import SerialTask from './serial-task'
/**
 * 
 * @param {*} message 
 * @param {*} type date | number|string|array
 * @param {*} trigger blur | change
 * @returns 
 */
export const vRequired = function (message, type = "string", trigger) {
    return { required: true, type, message: message, trigger: (trigger || 'blur'), whitespace: true, fullField: ' ', transform:function(v){
        if ((type === 'number' || type == 'string') && typeof v === 'string' && !v.trim()){
            v = null;
        }
        if (type === 'array' && Array.isArray(v) && v.length === 0){
            v = null;
        }
        if (type === 'number'){
            const iv = parseInt(v)
            if (!isNaN(iv)){
                return +v
            }
        }
        return v
    } }
}

export const vRange = function (message, type = "string", trigger, min, max) {
    if (type === 'date') {
        min = !!min ? resolveDyncDate(min) : undefined
        max = !!max ? resolveDyncDate(max) : undefined
    }
    return {
        fullField: ' ', 
        min, max, message: message, trigger: (trigger || 'blur'), type, transform(v) {
            if (typeof v === 'string' && v && type === 'number') {
                return +v
            }
            if (type === 'date') {
                return moment(v)._d
            }
            return v
        }
    }
}

/**
 * 自定义校验逻辑
 * validator: async function(rule(规则配置), value(值), cb(回调函数), source(数据源), options(配置)){
 *        如果校验失败
 *         return Promise.reject('错误描述')
 *        校验成功
 *         啥也不处理
 *   
 * }
 * @returns 
 */
export const vValidator = function (validator, trigger) {
    return { validator, trigger: (trigger || 'blur'),fullField: ' ', }
}

export const vPattern = function (message, pattern, trigger) {
    return { message, pattern, trigger: (trigger || 'blur'), type: 'pattern', fullField: ' ', }
}

/**
 * 组合验证业务
 * vCompose({'0': vRequired(), ..... }, true|false, ?)
 * @param {*} options 
 * @param {*} fromModel true: 从根对象model取值 false : 冲当前formitem prop取值
 * @param {*} trigger 
 * @returns 
 */
export const vCompose = function (_options, fromModel, trigger) {
    _options = _options || {}
    return {
        fullField: ' ',
        trigger: (trigger || 'blur'),
        // vCompose validator
        validator: function (rule, value, cb, source, options) {
            value = value || {}
            source = source || {}
            const keys = Object.keys(_options)
            new SerialTask(keys.length, (v) => {
                if (v) { 
                    cb(v)
                } else {
                    cb()
                }
            }).start((index) => {
                return new Promise((resolve, reject) => {
                    const v = objectProperty(fromModel === true ? source : value, keys[index])
                    new Schema({
                        input: _options[keys[index]]
                    }).validate({ ...(source || {}), input: v }, (err) => {
                        if (err) { 
                            // 验证失败
                            reject(err)
                        } else {
                            // 验证成功
                            resolve(null)
                        }
                    })
                })
            })
        }
    }
}
